import { validBlank } from "./plugFun"
import { getDefaultRule } from "./verifun"

function getFormRule(type = 'required', config = {}) {
    return Object.assign({}, getDefaultRule(type), config)
}
function getDefaultConfig(type, field, config = {}) {
    const _config = {
        required: true,
        trigger: 'change'
    }
    switch (type) {
        case 'input':
            _config.message = `请输入${field}`
            break
        case 'select':
            _config.message = `请选择${field}`
            break
        case 'upload':
            _config.message = `请上传${field}`
            break
        case 'no-required':
            _config.required = false
            break
        case 'required':
            _config.message = `${field}不能为空`
            break
        case 'maxlength':
            _config.message = `${field}长度不能大于${config.maxlength}`
            break
        case 'minlength':
            _config.message = `${field}长度不能小于${config.minlength}`
            break
        case 'max':
            _config.message = `${field}数额不能大于${config.max}`
            break
        case 'min':
            _config.message = `${field}数额不能小于${config.min}`
            break
        case 'number':
            _config.message = `${field}必须为数字`
            break
        case 'integer':
            _config.message = `${field}必须为整数`
            break
        case 'price':
            _config.message = `${field}填写有误`
            break
        case 'positive':
            _config.message = `${field}必须为正数`
            break
        case 'duplic':
            _config.message = `${field}不能重复`
            break
        case 'compare':
            _config.message = `请输入正确的${field}`
            break
    }
    return _config
}

/**
 * 对 getFormRule 进行了一层封装，增加了一些默认的配置
 * @param {String} type
 *  input: 输入框校验
 *  select: 下拉选择框校验
 *  upload: 上传
 *  其他参考 getFormRule
 * @param {String} field 校验的字段名称
 * @param {Object} config 参考 getFormRule，会覆盖默认的配置
 */
function getRules(type, field, config = {}) {
    config = Object.assign({}, getDefaultConfig(type, field, config), config)
    // 更改type值
    type = ['input', 'select', 'upload'].includes(type) ? 'required' : type
    let rules = []
    if (type === 'required') {
        rules = [{ validator: validBlank }, getFormRule(type, config)]
        rules[0].newMessage = field
    } else {
        if (config.required === 'no-required') {
            const _config = Object.assign({}, config, getDefaultConfig('no-required', field))
            rules.unshift(getFormRule(type, _config))
        } else {
            rules = [getFormRule(type, config)]
        }
    }
    if (type === 'positive') {
        const _config = Object.assign({}, config, getDefaultConfig('number', field))
        // 以 _config 为最后合并对象
        rules.unshift(getFormRule('number', _config))
    }
    if (type !== 'required' && config.required) {
        const _config = Object.assign({}, config, getDefaultConfig('required', field))
        rules.unshift(getFormRule('required', _config))
    }
    // console.log('rules',rules);
    return rules
}

export default getRules
